﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Kinect;
using System.Speech.Recognition;
using System.Speech.AudioFormat;

namespace FGP_Kpaint
{
    class SpeechToText
    {
        private SpeechRecognitionEngine RecognitionEngine;
        MainWindow main;
        public SpeechToText(MainWindow mains)
        {
            this.main = mains;
            RecognitionEngine = new SpeechRecognitionEngine();
            main.textBlock1.Text = "initializing";

            /*RecognitionEngine.SetInputToDefaultAudioDevice();
            RecognitionEngine.LoadGrammar(new DictationGrammar());
            RecognitionEngine.RecognizeAsync(RecognizeMode.Multiple);
            RecognitionEngine.SpeechRecognized += (s, args) => MessageBox.Show(args.Result.Text);*/
        }

        private static RecognizerInfo GetKinectRecognizer()
        {
            Func<RecognizerInfo, bool> matchingFunc = r =>
            {
                string value;
                r.AdditionalInfo.TryGetValue("Kinect", out value);
                return "True".Equals(value, StringComparison.InvariantCultureIgnoreCase) && "en-US".Equals(r.Culture.Name, StringComparison.InvariantCultureIgnoreCase);
            };

            return SpeechRecognitionEngine.InstalledRecognizers().Where(matchingFunc).FirstOrDefault();
        }

        public void start() {
    
RecognitionEngine.SetInputToDefaultAudioDevice(); 
RecognitionEngine.LoadGrammar(new DictationGrammar());

main.textBlock1.Text = "loaded grammar.. speak";
RecognitionEngine.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(RecognitionEngine_SpeechRecognized);
RecognitionEngine.SpeechHypothesized += new EventHandler<SpeechHypothesizedEventArgs>(RecognitionEngine_SpeechHypothesized);
RecognitionEngine.SpeechRecognitionRejected += new EventHandler<SpeechRecognitionRejectedEventArgs>(RecognitionEngine_SpeechRecognitionRejected);
RecognitionEngine.SpeechDetected += new EventHandler<SpeechDetectedEventArgs>(RecognitionEngine_SpeechDetected);
            RecognitionEngine.RecognizeAsync(RecognizeMode.Multiple);
        }

        void RecognitionEngine_SpeechDetected(object sender, SpeechDetectedEventArgs e)
        {
            main.textBlock1.Text = "detected  " + e.AudioPosition.Seconds.ToString();
        }

        void RecognitionEngine_SpeechRecognitionRejected(object sender, SpeechRecognitionRejectedEventArgs e)
        {
            main.textBlock1.Text = "rejected  " +e.Result.Confidence + "   " + e.Result.Text;
        }

        void RecognitionEngine_SpeechHypothesized(object sender, SpeechHypothesizedEventArgs e)
        {
           // if (e.Result.Confidence > 0.2)
            main.textBlock1.Text = "hypo  " + e.Result.Confidence + "   "+ e.Result.Text;
        }

        void RecognitionEngine_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
            foreach (RecognizedWordUnit word in e.Result.Words)
            {
                if(e.Result.Confidence > 0.5)
                main.textBlock1.Text = "recognized  " + e.Result.Confidence + "   "+ word.Text;
                // Console.Write(“{0} “, word.Text); 
            } 
        }
    }
}
